{extend name="public/base"/}
{block name="body"}
<style type="text/css">
</style>
<title>编辑楼幢</title>
</head>
<body>
<article class="page-container">
	<form class="form form-horizontal section" id="form-admin-add">
		<section>
			<input type="hidden" class="input-text" value="{$data.id}" id="id" name="id">
			<div class="row cl">
				<label class="form-label col-xs-3 col-sm-2">GUID：</label>
				<div class="formControls col-xs-8 col-sm-9">
					<input type="text" class="input-text" value="{$data.guid}" placeholder="guid" id="guid" name="guid">
				</div>
			</div>
			<div class="row cl">
				<label class="form-label col-xs-3 col-sm-2">行政区域：</label>
				<div class="formControls col-xs-8 col-sm-9">
					<input type="text" class="input-text" value="{$data.city}" placeholder="湖北省武汉市" id="city" name="city">
				</div>
			</div>
			<div class="row cl">
				<label class="form-label col-xs-3 col-sm-2">街道巷：</label>
				<div class="formControls col-xs-8 col-sm-9">
					<input type="text" class="input-text" value="{$data.street}" placeholder="解放大道" id="street" name="street">
				</div>
			</div>
			<div class="row cl">
				<label class="form-label col-xs-3 col-sm-2">门牌：</label>
				<div class="formControls col-xs-8 col-sm-9">
					<input type="text" class="input-text" value="{$data.plate}" placeholder="123号" id="plate" name="plate">
				</div>
			</div>
			<div class="row cl">
				<label class="form-label col-xs-3 col-sm-2">小区：</label>
				<div class="formControls col-xs-8 col-sm-9">
					<input type="text" class="input-text" value="{$data.estate}" placeholder="三街" id="estate" name="estate">
				</div>
			</div>
			<div class="row cl">
				<label class="form-label col-xs-3 col-sm-2">楼幢：</label>
				<div class="formControls col-xs-8 col-sm-9">
					<input type="text" class="input-text" value="{$data.block}" placeholder="1号" id="block" name="block">
				</div>
			</div>
			<div class="row cl">
				<label class="form-label col-xs-3 col-sm-2">二维码：</label>
				<div class="formControls col-xs-8 col-sm-9">
	                <div style="position:relative;" id="drop_qrcode"></div>
				</div>
			</div>
			<div class="row cl">
				<label class="form-label col-xs-3 col-sm-2">号牌：</label>
				<div class="formControls col-xs-8 col-sm-9">
	                <div style="position:relative;" id="drop_area"></div>
	                <p style="color:#ccc;font-size:12px;">建议尺寸宽高:2:1</p>
				</div>
			</div>
		</section>
		<div class="row cl">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
				<input id="submit" class="btn btn-primary radius" type="button" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
			</div>
		</div>
	</form>
</article>
<script type="text/javascript" src="/static/lib/jquery.validation/1.14.0/jquery.validate.js"></script> 
<script type="text/javascript" src="/static/lib/jquery.validation/1.14.0/validate-methods.js"></script> 
<script type="text/javascript" src="/static/lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript" src="/static/js/upload.js"></script>
<script type="text/javascript">

var formDataimg = new FormData();
var formDataqrcode = new FormData();
var img='';
var qrcode='';
$(function(){
	$('#drop_area div img').attr('src','{$data.img}');
	$('#drop_area div img').attr('onerror',"javascript:this.src='/static/img/upload.png'");
	if($("#drop_area div img").attr('src')=='/static/img/upload.png'){
		img='';
	}else{
		img= $("#drop_area div img").attr('src');
	}
	$('#drop_qrcode div img').attr('src','{$data.qrcode}');
	$('#drop_qrcode div img').attr('onerror',"javascript:this.src='/static/img/upload.png'");
	if($("#drop_qrcode div img").attr('src')=='/static/img/upload.png'){
		qrcode='';
	}else{
		qrcode= $("#drop_qrcode div img").attr('src');
	}
});

var dragImgUpload = new DragImgUpload("#drop_area",{
    callback:function (files) {
        //回调函数，可以传递给后台等等
        if(files[0].size<3145728){
	        var file = files[0];
	        formDataimg.append("file",file);
        }else{
        	alert('出错');
        	location.replace(location.href);
        }
    }
});
var dragImgUpload = new DragImgUpload("#drop_qrcode",{
    callback:function (files) {
        //回调函数，可以传递给后台等等
        if(files[0].size<3145728){
	        var file = files[0];
	        formDataqrcode.append("file",file);
        }else{
        	alert('出错');
        	location.replace(location.href);
        }
    }
});
$("#submit").click(function() {
	layer.load(1, {
	  shade: [0.1,'#fff'] //0.1透明度的白色背景
	});
	var id=$("input[name=id]").val();
	var guid=$("input[name=guid]").val();
	var city=$("input[name=city]").val();
	var street=$("input[name=street]").val();
	var plate=$("input[name=plate]").val();
	var estate=$("input[name=estate]").val();
	var block=$("input[name=block]").val();
	if(formDataimg.get("file")||formDataqrcode.get("file")){
		if(formDataimg.get("file")){
			console.log(1)
			$.ajax({
		          type: 'POST',
		          url: "{:url('uploadtmd')}",
		          data:formDataimg,
		          async:false,
		          contentType: false, /*不可缺*/
		          processData: false, /*不可缺*/
		          success: function(response){
		            if(response.code==200){
		            	img=response.photo;
		            }
		          },
		          error: function(err){
		          	console.log('formDataimg'+err)
		          }
		    })
		}
		if(formDataqrcode.get("file")){
			console.log(2)
		    $.ajax({
		          type: 'POST',
		          url: "{:url('uploadtmd')}",
		          data:formDataqrcode,
		          async:false,
		          contentType: false, /*不可缺*/
		          processData: false, /*不可缺*/
		          success: function(response){
		            if(response.code==200){
		            	qrcode=response.photo;
		            }
		          },
		          error: function(err){
		          	console.log('qrcode'+err)
		          }
		    })
		}
	    $.ajax({
		    type: 'POST',
	        async:false,
		    url: "{:url('block_edit')}",
		    data: {id:id,guid:guid,city:city,street:street,img:img,qrcode:qrcode,plate:plate,estate:estate,block:block},
		    success: function(resdata){
			console.log(3)
		        if(resdata.code==200){
					layer.msg(resdata.msg,{icon:1,time:1000});
					setTimeout("parent.location.reload()",1000);
		        }else{
					layer.msg(resdata.msg,{icon:2,time:3000});
		        }
		    },
		    error: function(){
				layer.msg('数据保存失败',{icon:2,time:3000});
		    }
		})
	}else{
		$.ajax({
		    type: 'POST',
		    url: "{:url('block_edit')}",
		    data: {id:id,guid:guid,city:city,street:street,img:img,qrcode:qrcode,plate:plate,estate:estate,block:block},
		    success: function(resdata){
		        if(resdata.code==200){
					layer.msg(resdata.msg,{icon:1,time:1000});
					setTimeout("parent.location.reload()",1000);
		        }else{
					layer.msg(resdata.msg,{icon:2,time:3000});
		        }
		    },
		    error: function(){
				layer.msg('数据保存失败',{icon:2,time:3000});
		    }
		})
	}
	
})
</script> 
{/block}